/**
 * @Author       : Hejh(3010733382@qq.com)
 * @Version      : V1.0
 * @Date         : 2024-01-17 10:59:06
 * @Description  : 权限接口的验证器
 */

const Joi = require('joi')
const R = require('@/models/Response')
const { ERROR_SYSTEM_CODE } = require('@/constants/errorCode')
const { IS_INTEGER } = require('@/constants/joiSchema')

const permissionSchema = {
  page: IS_INTEGER.min(1).default(1),
  pageSize: IS_INTEGER.min(1).default(999),
  type: IS_INTEGER.valid(0, 1) //0:系统权限，1-文档权限
}

/**
 * @description 权限列表查询的参数验证
 * @param {*} ctx
 * @param {*} next
 * @returns
 */
module.exports.listPermissionValidate = async (ctx, next) => {
  const schema = Joi.object({
    page: permissionSchema.page,
    pageSize: permissionSchema.pageSize,
    type: permissionSchema.type
  }).validate(ctx.query)

  if (schema.error) {
    ctx.body = R.error(ERROR_SYSTEM_CODE.PARAMS_SCHEMA_ERROR)
    return
  }
  ctx.query = schema.value
  await next()
}
